"""
Created on Mon Jan 18 15:50:27 2021
@author: lzt
"""
"""
Created on Sat Dec 26 11:32:28 2020
@author: lzt
"""

import numpy as np
import os
import pandas as pd
import sys
from sklearn import metrics
from sklearn.model_selection import train_test_split
from sklearn.utils import shuffle

# "python hello.py a b1 b2 c"

# args = [训练文件绝对路径：String， s1,s2,s3,]
version = sys.argv[-1]
parameters = list(sys.argv[2:-2])
# FPath = "/var/tmp/"
FPath = "./"
prefix = "tujunxiongniubi"
suffix = "menjinsuoniubi"


def mape(y_true, y_pred):
    return np.mean(np.abs((y_pred - y_true) / y_true)) * 100


def main():
    file_name = str(sys.argv[1])
    XX = pd.read_excel(file_name)
    X = pd.read_excel(file_name)

    X = shuffle(X, random_state=0)
    y = X[str(parameters[-1])]

    droplist = list(set(X.columns.tolist()) - set(parameters[:-1]))
    X = X.drop(droplist, axis=1)

    # 构造训练集、测试集
    x_train, x_test, y_train, y_test = train_test_split(X, y, random_state=0, train_size=0.9)

    from sklearn.model_selection import KFold
    import lightgbm as lgb

    # 训练集
    df_train = pd.concat([x_train, y_train], axis=1)
    # 训练列 
    df_train_columns = df_train.columns.tolist()[:-1]
    print('df_train_columns', df_train_columns)

    # 测试集
    df_test = pd.concat([x_test, y_test], axis=1)
    name = df_test.columns
    df_test.columns = df_test.columns

    # 测试列 cvff-surface-e
    target = df_train[parameters[-1]]
    param = {'num_leaves': 31,
             'min_data_in_leaf': 30,
             'objective': 'regression',
             'max_depth': -1,
             'learning_rate': 0.01,
             "min_child_samples": 20,
             "boosting": "gbdt",
             "feature_fraction": 0.9,
             "bagging_freq": 1,
             "bagging_fraction": 0.9,
             "bagging_seed": 11,
             "metric": 'mae',
             "lambda_l1": 0.1,
             "verbosity": -1,
             "nthread": 8,
             "random_state": 666}
    folds = KFold(n_splits=10, shuffle=True, random_state=666)
    oof = np.zeros(len(df_train))
    predictions = np.zeros(len(df_test))
    feature_importance_df = pd.DataFrame()
    validation_predictions = 0
    train_predictions = 0
    for fold_, (trn_idx, val_idx) in enumerate(folds.split(df_train, df_train[parameters[-1]].values)):
        print("fold {}".format(fold_))
        trn_data = lgb.Dataset(df_train.iloc[trn_idx][df_train_columns], label=target.iloc[trn_idx])
        val_data = lgb.Dataset(df_train.iloc[val_idx][df_train_columns], label=target.iloc[val_idx])
        num_round = 1000
        # 训练
        clf = lgb.train(param, trn_data, num_round, valid_sets=[trn_data, val_data], verbose_eval=100,
                        early_stopping_rounds=100)
        oof[val_idx] = clf.predict(df_train.iloc[val_idx][df_train_columns], num_iteration=clf.best_iteration)
        print('here is oof', oof[val_idx])
        print('df train result', df_train.iloc[val_idx][parameters[-1]])
        oof[trn_idx] = clf.predict(df_train.iloc[trn_idx][df_train_columns], num_iteration=clf.best_iteration)
        train_predictions += metrics.mean_absolute_error(df_train.iloc[trn_idx][parameters[-1]],
                                                         oof[trn_idx]) / folds.n_splits
        validation_predictions += metrics.mean_absolute_error(df_train.iloc[val_idx][parameters[-1]],
                                                              oof[val_idx]) / folds.n_splits

        fold_importance_df = pd.DataFrame()
        fold_importance_df["Feature"] = df_train_columns
        fold_importance_df["importance"] = clf.feature_importance()
        fold_importance_df["fold"] = fold_ + 1
        feature_importance_df = pd.concat([feature_importance_df, fold_importance_df], axis=0)
        predictions += clf.predict(df_test[df_train_columns], num_iteration=clf.best_iteration) / folds.n_splits
        print('prediction shape', predictions.shape)

    # 模型保存

    feature_name = file_name.split("/")[-1][:-5] + "-model-" + version + ".txt"
    feature_path = os.path.join(FPath, feature_name)
    clf.save_model(feature_path)
    print(prefix + feature_path + suffix)


if __name__ == "__main__":
    main()
